package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.utils.Preferences;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.SpApplication;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.DHomeContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.DetaillnterfaceContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.DHomePresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.DetaillnterfacePresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.DHomeAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/19 14:25
 * 功能描述： 搜索界面
 * 联系方式：1037438704@qq.com
 */
public class SearchActivity extends BaseActivity implements DHomeContract, DetaillnterfaceContract {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_search)
    EditText textSearch;
    @BindView(R.id.text_right)
    TextView textRight;
    private DHomeAdapter dHomeAdapter;
    private DHomePresenter dHomePresenter;
    private DetaillnterfacePresenter presenter;
    @BindView(R.id.frame_gone)
    FrameLayout frame_gone;
    public String sex;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_search;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        initView();
        initData();
    }

    private void initView() {
        sex = Preferences.getInstance().getString(me, "user", "sex");
        if (sex.equals("1")) {
            textSearch.setHint("搜索女士昵称");
        } else {
            textSearch.setHint("搜索男士昵称");
        }

        dHomePresenter = new DHomePresenter(this);
        recyclerView.setVisibility(View.GONE);
        presenter = new DetaillnterfacePresenter(me, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dHomeAdapter = new DHomeAdapter(this);
        recyclerView.setAdapter(dHomeAdapter);
    }

    public static void start(Context context) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent(context, SearchActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @OnClick({R.id.image_left_finish, R.id.text_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_right:
                if (TextUtils.isEmpty(textSearch.getText().toString().trim())) {
                    ToastUtil.show("搜索内容不能为空");
                    return;
                }
                http();
                break;
            default:
        }
    }

    @Override
    public void Success(List<Map<String, String>> data) {
        if (data == null || data.size() == 0) {
            recyclerView.setVisibility(View.GONE);
            frame_gone.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            frame_gone.setVisibility(View.GONE);
            dHomeAdapter.clear();
            dHomeAdapter.addAll(data);
        }

    }

    @Override
    public void DetaSuccess(Map<String, String> data) {

    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void isPay(Map<String, String> data, String type, String do_type) {

    }

    @Override
    public void memberInfor() {
        http();
    }

    private void http() {
        if (TextUtils.isEmpty(textSearch.getText().toString().trim())) {
            return;
        }
        dHomePresenter.getIndex2(token, sex,
                SpApplication.LONGITUDE, SpApplication.LATITUDE,
                "", "", "", textSearch.getText().toString().trim());
    }

    private void initData() {
        dHomeAdapter.setmOnClick(new DHomeAdapter.AdpItemOnClick() {
            @Override
            public void onClick(int position, Map<String, String> data, int type) {
                if (type == 0) {
                    presenter.addMylike(token, data.get("id"));
                } else {
                    DetailInterfaceActivity.start(me, data.get("id"));
                }
            }
        });
    }

}
